﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace QuanLyKhachSan
{
  
    public partial class FmHoaDon : Form
    {
        int maphieu;
        bool khachnuocngoai;
        int songay;
        int soluongkhachhang;
        int giaphong;
        int tienphong;
        int tongtien;
        HoaDonDTO hoadon = new HoaDonDTO();
        public event FireEventThanhToan EventThanhToan = null;
        public FmHoaDon(string maPhong)
        {
            InitializeComponent();
            lbMaPhong.Text = maPhong.ToString();
            
        }

        private void FmHoaDon_Load(object sender, EventArgs e)
        {
            LoaHoaDon();
            LoadKhachHang();
            

        }
        void LoaHoaDon()
        {
            PhieuThueBUS busphieu = new PhieuThueBUS();
            maphieu = busphieu.LayPhieuThueMaxTheoPhong(lbMaPhong.Text);

            HoaDonBUS bus = new HoaDonBUS();
            DataRow row = bus.LayThongTinHoaDonTuPhieuThue(maphieu).Rows[0];
            giaphong = Int32.Parse(row["Gia"].ToString());
            lbGia.Text = string.Format("{0:0,0}",row["Gia"])+" VNĐ";
            lbLoaiPhong.Text = row["TenLoaiPhong"].ToString();
            lbTenPhong.Text = row["TenPhong"].ToString();
            lbVao.Text = row["NgayThue"].ToString();
            lbRa.Text = DateTime.Now.ToString();
            DateTime dtvao= DateTime.Parse(lbVao.Text);
            /////......................................................////
            PhieuThueChiTietBUS busct = new PhieuThueChiTietBUS();
            dt = busct.loadChiTietTheoMaPhieu(maphieu);
            soluongkhachhang = dt.Rows.Count;
            lbsoluongkhach.Text = soluongkhachhang.ToString();
            ThamSoDTO thamsodto = LayThamSo();
            //..............................................................//
            khachnuocngoai = isKhachNuocNgoai(thamsodto.Loaikh);
            if (khachnuocngoai)
            {
                lbYesNo.Text = " Có Khách nước ngoài nhân hệ số 1.5";
            }
            else
                lbYesNo.Text = "";
            songay = SoNgay(dtvao, DateTime.Now);
            lbSongay.Text = songay.ToString();
            tienphong = songay * giaphong;
            lbTienPhong.Text = String.Format("{0:0,0}",tienphong)+" VNĐ";
            tongtien = songay * TinhTien(thamsodto);
            lbThanhTien.Text = String.Format("{0:0,0}", tongtien)+" VNĐ";

            // Lay thong tin hoa don

        }
  
        ThamSoDTO LayThamSo()
        {
            
            ThamSoBUS bus = new ThamSoBUS();
            return bus.LayThamSo();
            
        }
        DataTable dt = new DataTable();
        void LoadKhachHang()
        {
           
            cbKH.DataSource = dt;
            cbKH.DisplayMember = "TenKH";
            cbKH.ValueMember = "MaKH";
           
        }
        int TinhTien(ThamSoDTO dtp)
        {
            int gia = giaphong;
            int kq = 1;
            if (khachnuocngoai == true)
            {
                kq = Convert.ToInt32(gia * dtp.Heso);
            }
            else
                kq = gia;
           
            if (CoPhuThusoluong(soluongkhachhang) == true)
            {
                kq += kq * dtp.Phuthu/100;
            }
            return kq;
            
            
           
        }
        bool isKhachNuocNgoai(int maloaikh)
        {
         
            foreach(DataRow row in dt.Rows)
            {
                if (Int32.Parse(row["MaLoaiKH"].ToString()) == maloaikh)
                    return true;
            }
            return false;
        }
        bool CoPhuThusoluong(int soluongkhach)
        {
            int i = dt.Rows.Count;
            if (i == 3)
                return true;
            return false;
       
        }
        int SoNgay(DateTime Ngayvao,DateTime NgayRa)
        {
            int songay = ((TimeSpan)(NgayRa - Ngayvao)).Days;
            return songay;

        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            ThemHoaDon();
        }
        HoaDonDTO LayThongTinHoaDon()
        {
           
            hoadon.Diachikh = txtDiaCHi.Text;
            string time = DateTime.Now.ToString();
            DateTime tg = DateTime.Parse(time);
            hoadon.Ngaylap = tg;
            hoadon.Maphieu = maphieu;
            hoadon.Thanhtien = tongtien;
            hoadon.Tenkh = cbKH.SelectedText;
            return hoadon;
        }
        void ThemHoaDon()
        {
            
            HoaDonBUS bus = new HoaDonBUS();
            HoaDonChiTietBUS buschitiet = new HoaDonChiTietBUS();
            List<HoaDonChiTietDTO> list = new List<HoaDonChiTietDTO>();
            HoaDonChiTietDTO dto = new HoaDonChiTietDTO();
            dto.Maphong =Int32.Parse( lbMaPhong.Text);
            dto.Dongia = Int32.Parse(giaphong.ToString());
            dto.Ngavao =DateTime.Parse( lbVao.Text);
            dto.Ngayra = DateTime.Parse(lbRa.Text);
            dto.Songay = Int32.Parse( lbSongay.Text);
            dto.Thanhtien = tongtien;
            dto.Maphieu = maphieu;
            list.Add(dto);
            if (bus.ThemHoaDon(LayThongTinHoaDon()) != 0)
            {
                int mahoadon = bus.LayMaxHoaDon();

                if (mahoadon!= 0)
                {
                    if (EventThanhToan != null)
                    {

                        buschitiet.THemChiTIetHoaDon(list, mahoadon);
                        MessageBox.Show("Thanh Toán Thành công");
                        EventThanhToan(this, new ThanhToanThanhCong { MaPhong = lbMaPhong.Text });
                     
                    }
                }

            }
            else
                MessageBox.Show("Thanh Toán Thất bại");
        }
    }
}
